﻿using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;
using System.Collections.Generic;
using BenzInfo.Hockey.Grenadiers.Utils;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{
    public class PartnersController : BaseController
    {
        private GrenadiersContext db = new GrenadiersContext();

        private const string BASE_PATH = "~/Content/images/partners";
        //
        // GET: /Partners/

        public ViewResult Index()
        {
            return View(db.Partners.ToList());
        }

        //
        // GET: /Partners/Create

        public ActionResult Create()
        {
            return PartialView();
        } 

        //
        // POST: /Partners/Create

        [HttpPost]
        public ActionResult Create(Partner partner, HttpPostedFileBase inputFile)
        {
            if (inputFile != null)
            {
                using (Image img = Image.FromStream(inputFile.InputStream))
                {
                    string ext = inputFile.FileName.Split('.')[inputFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{0:000}.{1}", DateTime.Now.Ticks, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), filename);

                    ImageHelper.Save(img, filepath, ext);

                    partner.Picture = filename;
                }
            }

            if (ModelState.IsValid)
            {
                partner.Position = db.Partners.Count() + 1;

                db.Partners.Add(partner);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return PartialView(partner);
        }

        //
        // GET: /Partners/EditPosition

        public void EditPosition(int[] items)
        {
            int i = 1;
            foreach (int id in items)
            {
                Partner p = db.Partners.Find(id);
                p.Position = i++;
                db.Entry(p).State = EntityState.Modified;
            }

            db.SaveChanges();
        }

        //
        // GET: /Partners/Edit/5
 
        public ActionResult Edit(int id)
        {
            Partner partner = db.Partners.Find(id);
            return PartialView(partner);
        }

        //
        // POST: /Partners/Edit/5

        [HttpPost]
        public ActionResult Edit(Partner partner, HttpPostedFileBase inputFile, string nopicture)
        {
            if (nopicture == "on")
            {
                partner.Picture = null;
            }
            else if (inputFile != null)
            {
                using (Image img = Image.FromStream(inputFile.InputStream))
                {
                    string ext = inputFile.FileName.Split('.')[inputFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{1}_{0:000}.{2}", DateTime.Now.Ticks, partner.Id, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), filename);

                    ImageHelper.Save(img, filepath, ext);

                    partner.Picture = filename;
                }
            }

            if (ModelState.IsValid)
            {
                db.Entry(partner).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return PartialView(partner);
        }

        //
        // GET: /Partners/Delete/5
 
        public ActionResult Delete(int id)
        {
            Partner partner = db.Partners.Find(id);
            return PartialView(partner);
        }

        //
        // POST: /Partners/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Partner partner = db.Partners.Find(id);
            db.Partners.Remove(partner);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}